﻿using DAL.Register;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    /// <summary>
    /// 图书借还类
    /// 
    /// </summary>
    public class BorrowReturnMDAL
    {
        /// <summary>
        /// 根据读者编号查询读者信息
        /// </summary>
        /// <param name="ReaderId">读者编号</param>
        /// <returns></returns>
        public DataTable getReturnDAL(string ReaderId)
        {
            if (!string.IsNullOrEmpty(ReaderId))
            {
                string sql = @"SELECT ID 编号,ReaderID 读者编号,ReadName 读者姓名,EMail 读者邮箱,Address 读者住址,Mobile 读者手机号码  FROM ReaderInfo WHERE ReaderID =@ReaderID ";
                SqlParameter[] sqlp = {
                    new SqlParameter("@ReaderID",ReaderId),
                };
                return SQLHelper.Query(sql, sqlp);
            }
            return null;
        }


        /// <summary>
        /// 图书列表及搜索
        /// </summary>
        /// <param name="BookID">图书id</param>
        /// <returns></returns>
        public DataTable getBookDAL(string BookID)
        {
            if (BookID == "") BookID = null;
            string sqls = BookID != null ? " and b.BookID = @BookID" : "";
            string sql = @"SELECT b.ID 编号,b.Name 图书名称,a.CategoryName 图书类别,b.Author 作者,b.Money 价格,b.BookID 图书id,c.Description 借出状态 
                            FROM BookCategory as a
                            INNER JOIN BookInfo  b
                            ON a.ID= b.CategoryType
                            LEFT OUTER JOIN BookState  c
                            ON c.ID=b.State
                            WHERE b.State!=1 " + sqls;
            SqlParameter[] sqlp = {
                    new SqlParameter("@BookID",BookID),
                };
            return SQLHelper.Query(sql, BookID != null ? sqlp : null);
        }

        /// <summary>
        /// 根据读者id查询借还记录
        /// </summary>
        /// <param name="ReaderID"></param>
        /// <returns></returns>
        public DataTable getBorrowReturnDAL(string ReaderID)
        {
            if (ReaderID == "") ReaderID = null;
            string sqls = ReaderID != null ? " and a.ReaderID = @ReaderID" : "";
            string sql = @"SELECT a.ID ID,b.BookID 编号,b.Name 图书名称,a.ReaderID 借书人ID ,a.BorrowTime 借书时间
                            FROM BookBorrowReturnInfo	 as a
                            INNER JOIN BookInfo  b
                            ON a.BookID= b.ID 
                            WHERE a.ReturnTime IS NULL " + sqls;
            SqlParameter[] sqlp = {
                    new SqlParameter("@ReaderID",ReaderID),
                };
            return SQLHelper.Query(sql, ReaderID != null ? sqlp : null);
        }

        
        /// <summary>
        /// 借书
        /// </summary>
        /// <param name="BookID">图书id</param>
        /// <param name="ReturnID">读者</param>
        /// <returns></returns>
        public bool geAddBorrowDAL(string BookID,string ReturnID)
        {
            string[] sql1 = { 
            //修改BookInfo的图书状态
            " UPDATE BookInfo SET State=1 WHERE ID=@ID",
            //添加借书记录
            "INSERT INTO BookBorrowReturnInfo(BookID,ReaderID,BorrowTime) VALUES (@BookID,@ReaderID,@BorrowTime)"
            };
            #region SqlParameter
            SqlParameter[] pars0 =
             {
                      new SqlParameter("@ID",BookID),
             };
            SqlParameter[] pars2 =
                {
                   new SqlParameter("@BookID",BookID),
                   new SqlParameter("@ReaderID",ReturnID),
                   new SqlParameter("@BorrowTime",DateTime.Now),
             };

            SqlParameter[][] pars =
            {
               pars0,pars2
            };
            #endregion
            return SQLHelper.UOWBorrow(sql1, pars);
        }

        /// <summary>
        /// 还书
        /// </summary>
        /// <param name="BookID"></param>
        /// <param name="BorrowReturnID"></param>
        /// <returns></returns>
        public bool ReturnBookDAL(string BookID, string BorrowReturnID)
        {
            string[] sql1 = { 
            //修改BookInfo的图书状态
            " UPDATE BookInfo SET State=0 WHERE BookID=@BookID",
            //修改借书记录
            "UPDATE BookBorrowReturnInfo SET ReturnTime=@ReturnTime WHERE ID=@ID"
            };
            #region SqlParameter
            SqlParameter[] pars0 =
             {
                      new SqlParameter("@BookID",BookID),
             };
            SqlParameter[] pars2 =
                {
                   new SqlParameter("@ID",BorrowReturnID),
                   new SqlParameter("@ReturnTime",DateTime.Now),
             };

            SqlParameter[][] pars =
            {
               pars0,pars2
            };
            #endregion
            return SQLHelper.UOWBorrow(sql1, pars);
        }

    }
}
